package ysu.sxm.ptjc.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.AccountInfoDao;
import ysu.sxm.ptjc.Dao.ProRelationInfoDao;
import ysu.sxm.ptjc.Dao.ProduceStatisticalInfoDao;
import ysu.sxm.ptjc.Domin.DateRange;
import ysu.sxm.ptjc.Domin.InvalidationOut;
import ysu.sxm.ptjc.Domin.InvalidationReq;
import ysu.sxm.ptjc.Service.InvalidationService;
import ysu.sxm.ptjc.Utill.Utills;
import ysu.sxm.ptjc.pojo.AccountInfo;
import ysu.sxm.ptjc.pojo.ProRelationInfo;
import ysu.sxm.ptjc.pojo.ProduceStatisticalInfo;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
@Service
public class InvalidationServiceImpl implements InvalidationService {
    @Autowired
    ProRelationInfoDao proRelationInfoDao;
    @Autowired
    AccountInfoDao accountInfoDao;
    @Autowired
    ProduceStatisticalInfoDao produceStatisticalInfoDao;

    @Override
    public List<InvalidationOut> dealWithInvReq(InvalidationReq invalidationReq) {
        List<ProRelationInfo> lists =proRelationInfoDao.getByInvalidationReq(invalidationReq);
        List<Date> dates=new ArrayList<Date>();
        List<InvalidationOut>invalidationOuts=new ArrayList<InvalidationOut>();
        if (invalidationReq.getDates()!=null){
            dates=invalidationReq.getDates();
            for (ProRelationInfo p:lists) {
                List<ProduceStatisticalInfo> produceStatisticalInfos = produceStatisticalInfoDao.getByNumAndDate(p, dates,invalidationReq);
                List<AccountInfo>accountInfos=new ArrayList<AccountInfo>();
                if(!produceStatisticalInfos.isEmpty()) {
                     accountInfos = accountInfoDao.findAccountByProduceStatisticalInfos(produceStatisticalInfos);
                }
                for(int i=0;i<produceStatisticalInfos.size();i++) {
                    InvalidationOut invalidationOut = new InvalidationOut();
                    invalidationOut.setColor(p.getColor());invalidationOut.setCarType(p.getCarTypeId());invalidationOut.setProduceName(produceStatisticalInfos.get(i).getProduceName());
                    invalidationOut.setProduceQuantity(produceStatisticalInfos.get(i).getProduceQuantity().intValue());invalidationOut.setQualifyNum(produceStatisticalInfos.get(i).getQualifyNum().intValue());
                    invalidationOut.setScrapNum(produceStatisticalInfos.get(i).getScrapNum().intValue());invalidationOut.setReworkRate(accountInfos.get(i).getReworkRate());
                    invalidationOut.setScrapRate(accountInfos.get(i).getScrapRate());invalidationOut.setUnqualifiedRate(accountInfos.get(i).getUnqualified_Rate());
                    invalidationOut.setFirstpassRate(accountInfos.get(i).getFirstpassRate());invalidationOut.setTotalqualifiedRate(accountInfos.get(i).getTotalqualified_Rate());
                    invalidationOuts.add(invalidationOut);
                }
            }

        }
        else if(invalidationReq.getWeeks()!=null){
            List<DateRange> weekDays= Utills.getWeekDays(invalidationReq.getWeeks());
            for (ProRelationInfo p:lists) {
                for (DateRange weekday:weekDays){
                    List<ProduceStatisticalInfo> produceStatisticalInfos = produceStatisticalInfoDao.getByNumAndDateRange(p,weekday,invalidationReq);
                    List<AccountInfo> accountInfos=new ArrayList<AccountInfo>();
                    if(!produceStatisticalInfos.isEmpty()) {
//                    for(ProduceStatisticalInfo produceStatisticalInfo:produceStatisticalInfos){
//                        System.out.println(produceStatisticalInfo);
//                    }
                         accountInfos = accountInfoDao.findAccountByProduceStatisticalInfos(produceStatisticalInfos);
//                    for (AccountInfo accountInfo:accountInfos){
//                        System.out.println(accountInfo);
//                    }
                    }
                    for(int i=0;i<produceStatisticalInfos.size();i++) {
                        InvalidationOut invalidationOut = new InvalidationOut();
                        invalidationOut.setColor(p.getColor());invalidationOut.setCarType(p.getCarTypeId());invalidationOut.setProduceName(produceStatisticalInfos.get(i).getProduceName());
                        invalidationOut.setProduceQuantity(produceStatisticalInfos.get(i).getProduceQuantity().intValue());invalidationOut.setQualifyNum(produceStatisticalInfos.get(i).getQualifyNum().intValue());
                        invalidationOut.setScrapNum(produceStatisticalInfos.get(i).getScrapNum().intValue());invalidationOut.setReworkRate(accountInfos.get(i).getReworkRate());
                        invalidationOut.setScrapRate(accountInfos.get(i).getScrapRate());invalidationOut.setUnqualifiedRate(accountInfos.get(i).getUnqualified_Rate());
                        invalidationOut.setFirstpassRate(accountInfos.get(i).getFirstpassRate());invalidationOut.setTotalqualifiedRate(accountInfos.get(i).getTotalqualified_Rate());
                        invalidationOuts.add(invalidationOut);
                    }
                }
            }


        }else {
            List<DateRange> monthDays= Utills.getMonthDays(invalidationReq.getMonths());
            for (ProRelationInfo p:lists) {
                for (DateRange monthDay:monthDays){
                    List<ProduceStatisticalInfo> produceStatisticalInfos = produceStatisticalInfoDao.getByNumAndDateRange(p,monthDay,invalidationReq);
                    List<AccountInfo>accountInfos=new ArrayList<AccountInfo>();
                    if (!produceStatisticalInfos.isEmpty()) {
                        accountInfos = accountInfoDao.findAccountByProduceStatisticalInfos(produceStatisticalInfos);
                    }
                    for(int i=0;i<produceStatisticalInfos.size();i++) {
                        InvalidationOut invalidationOut = new InvalidationOut();
                        invalidationOut.setColor(p.getColor());invalidationOut.setCarType(p.getCarTypeId());invalidationOut.setProduceName(produceStatisticalInfos.get(i).getProduceName());
                        invalidationOut.setProduceQuantity(produceStatisticalInfos.get(i).getProduceQuantity().intValue());invalidationOut.setQualifyNum(produceStatisticalInfos.get(i).getQualifyNum().intValue());
                        invalidationOut.setScrapNum(produceStatisticalInfos.get(i).getScrapNum().intValue());invalidationOut.setReworkRate(accountInfos.get(i).getReworkRate());
                        invalidationOut.setScrapRate(accountInfos.get(i).getScrapRate());invalidationOut.setUnqualifiedRate(accountInfos.get(i).getUnqualified_Rate());
                        invalidationOut.setFirstpassRate(accountInfos.get(i).getFirstpassRate());invalidationOut.setTotalqualifiedRate(accountInfos.get(i).getTotalqualified_Rate());
                        invalidationOuts.add(invalidationOut);
                    }
                }
            }
        }

        return invalidationOuts;
    }
}
